<?php
/**
 * In this File the class '\UF\IO\Path' is defined.
 *
 * @category   UniKap-Framework
 * @package    IO
 * @subpackage Core
 * @author     Ulf -UniKado- Kadner <ulfikado@gmail.com>
 * @since      2014-03-29 16:17
 * @version    0.1
 */

namespace UF\IO
{

    /**
     * A static helper class for paths.
     *
     * @since  v0.1
     */
    class Path
    {

        # <editor-fold defaultstate="collapsed" desc="- - - -   P U B L I C   S T A T I C   F I E L D S   - - - - - - - - - - - - - - -">

        /**
         * This folder separator is not used at current system
         *
         * @var char
         * @since  v0.1
         */
        public static $NoFolderSeparator;

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="- - - >   H I D D E N   C O N S T R U C T O R   - - - - - - - - - - - - - - - - -">

        /**
         * @since  v0.1
         */
        private function __construct() { }

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="- - - -   P U B L I C   S T A T I C   M E T H O D S   - - - - - - - - - - - - - -">

        /**
         * Combine 2 or 3 path elements with system depending directory separator.
         *
         * @param  string $basepath First path element
         * @param  string $next1    Second path element
         * @param  string $next2    Third, optional path element
         * @return string
         * @since  v0.1
         */
        public static function Combine( $basepath, $next1, $next2=null )
        {
            if ( empty( $next2 ) )
                return \rtrim( $basepath, "\r\n\t /\\" ) . \DIRECTORY_SEPARATOR . \ltrim( $next1, "\r\n\t /\\" );
            return \rtrim( $basepath, "\r\n\t /\\" ) . \DIRECTORY_SEPARATOR .
                \trim( $next1, "\r\n\t /\\" ) . \DIRECTORY_SEPARATOR . \ltrim( $next2, "\r\n\t /\\" );
        }

        /**
         * Normalize all directory separator to system depending separators.
         *
         * @param string $path
         * @return string
         * @since  v0.1
         */
        public static function Normalize( $path )
        {
            if ( \is_null( self::$NoFolderSeparator ) )
                self::$NoFolderSeparator = \IS_WIN ? '/' : '\\';
            return ( \DIRECTORY_SEPARATOR == '\\' )
                ? \trim( \str_replace( self::$NoFolderSeparator, \DIRECTORY_SEPARATOR, $path ), \DIRECTORY_SEPARATOR )
                : \rtrim( \str_replace( self::$NoFolderSeparator, \DIRECTORY_SEPARATOR, $path ), \DIRECTORY_SEPARATOR );
        }

        /**
         * Returns, if the defined path is a absolute path definition.
         *
         * @param  string $path
         * @return bool
         * @since  v0.1
         */
        public static function IsAbsolute( $path )
        {
            if ( \is_null( $path ) || \strlen( $path ) < 1 ) return false;
            if ( \DIRECTORY_SEPARATOR == '/' ) return $path[0] == '/';
            if ( \strlen($path) < 2 ) return false;
            return $path[1] == ':';
        }

        /**
         * Replace all backslashes with slashes.
         *
         * @param  string $path
         * @return string
         * @since  v0.1
         */
        public static function Unixize( $path )
        {
            if ( \is_null( $path ) ) return '';
            return \str_replace( '\\', '/', $path );
        }

        /**
         * Removes the current working directory from defined absolute path.
         *
         * @param  string $path
         * @return string
         * @since  v0.1
         */
        public static function RemoveWorkingDir( $path )
        {
            if ( \is_null( $path ) ) return '';
            $wd = '~^' . \preg_quote( self::Unixize( \getcwd() ) . '/' ) . '~';
            return \preg_replace( $wd, '', self::Unixize( $path ) );
        }

        # </editor-fold>

    }

}

